﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SuperLauncher.Entitys;
using SuperLauncher.Helpers;
using SL8.SL;
using SL8.SL.Commands;

namespace SuperLauncher.ViewModels
{
    public class JumpListItemInfoEditViewModel : SL8.SL.ViewModels.UIViewModelBase
    {
        public System.Collections.ObjectModel.ObservableCollection<CategoryInfo> CategoryInfos
        {
            get
            {
                return DataHelper.CategoryInfos;
            }
        }

        public EditStatusEnum EditStatus
        {
            get
            {
                return this.GetCacheValue(o => o.EditStatus, EditStatusEnum.Add);
            }

            set
            {
                this.SetCacheValue(o => o.EditStatus, value);
                this.RaisePropertyChanged(o => o.EditStatus);
            }
        }

        public bool IsClose
        {
            get
            {
                bool b = this.GetCacheValue(o => o.IsClose, false);
                return b;
            }

            set
            {
                this.SetCacheValue(o => o.IsClose, value);
                this.RaisePropertyChanged(o => o.IsClose);
            }
        }

        private ItemInfo editJumpListItemCache = null;

        public ItemInfo JumpListItem
        {
            get
            {
                return this.GetCacheValue(o => o.JumpListItem, new ItemInfo());
            }

            set
            {
                this.editJumpListItemCache = value;
                this.SetCacheValue(o => o.JumpListItem, new ItemInfo()
                {
                    CommandLineArguments = value.CommandLineArguments,
                    Description = value.Description,
                    LaunchingItemID = value.LaunchingItemID,
                    Path = value.Path,
                    StartDirectory = value.StartDirectory,
                    Title = value.Title
                });
            }
        }

        [Display(Description = "请输入应用程序分类", Name = "所属分类")]
        public string CategoryTitle
        {
            get
            {
                return this.GetCacheValue(o => o.CategoryTitle, default(string));
            }

            set
            {
                this.SetCacheValue(o => o.CategoryTitle, value);
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                return this.GetCacheValue(o => o.SaveCommand, new DelegateCommand(this.OnSaveCommand));
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return this.GetCacheValue(o => o.CancelCommand, new DelegateCommand(this.OnCancelCommand));
            }
        }

        public JumpListItemInfoEditViewModel()
        {
            DataHelper.SetOverlayIconSave();
        }

        private void OnCancelCommand(object arg)
        {
            this.IsClose = true;
            DataHelper.SetOverlayIconEmpty();
        }

        private void OnSaveCommand(object arg)
        {
            if ((bool)arg)
            {
                if (this.EditStatus == EditStatusEnum.Add)
                {
                    CategoryInfo category = null;
                    if (string.IsNullOrWhiteSpace(this.CategoryTitle))
                    {
                        category = this.CategoryInfos.FirstOrDefault(o => o.Title == DataHelper.NoCategoryName);
                        if (category == null)
                        {
                            category = new CategoryInfo()
                            {
                                Title = DataHelper.NoCategoryName
                            };

                            this.CategoryInfos.Add(category);
                        }
                    }
                    else
                    {
                        foreach (var item in this.CategoryInfos)
                        {
                            if (item.Title == this.CategoryTitle)
                            {
                                category = item;
                                break;
                            }
                        }

                        if (category == null)
                        {
                            category = new CategoryInfo()
                            {
                                Title = this.CategoryTitle
                            };

                            if (this.CategoryInfos.Count > 0)
                            {
                                this.CategoryInfos.Insert(this.CategoryInfos.Count - 1, category);
                            }
                            else
                            {
                                this.CategoryInfos.Add(category);
                            }
                        }
                    }

                    category.Items.Add(this.JumpListItem);
                    this.EditStatus = EditStatusEnum.Edit; 
                }
                else if (this.EditStatus == EditStatusEnum.Edit)
                {
                    this.editJumpListItemCache.CommandLineArguments = this.JumpListItem.CommandLineArguments;
                    this.editJumpListItemCache.Description = this.JumpListItem.Description;
                    this.editJumpListItemCache.LaunchingItemID = this.JumpListItem.LaunchingItemID;
                    this.editJumpListItemCache.Path = this.JumpListItem.Path;
                    this.editJumpListItemCache.StartDirectory = this.JumpListItem.StartDirectory;
                    this.editJumpListItemCache.Title = this.JumpListItem.Title;
                }

                this.IsClose = true;

                this.SaveToXml();
            }
        }

        private void SaveToXml()
        {
            DataHelper.SaveData(this.CategoryInfos);
            JumpListHelper.RefreshJumplist(this.CategoryInfos);
            DataHelper.SetOverlayIconAccept();
        }
    }
}
